from datetime import datetime

from fastapi import APIRouter, Depends, Request
from pydantic_validation_decorator import ValidateFields
from sqlalchemy.ext.asyncio import AsyncSession

from config.enums import BusinessType
from config.get_db import get_db
from module_admin.annotation.log_annotation import Log
from module_admin.aspect.interface_auth import CheckUserInterfaceAuth
from module_admin.entity.vo.user_vo import CurrentUserModel
from module_admin.service.login_service import LoginService
from module_basics.entity.vo.thesaurus_vo import ThesaurusPageQueryModel, ThesaurusModel, ThesaurusDataPageQueryModel, \
    DeleteThesaurusModel, ThesaurusDataModel, DeleteThesaurusDataModel
from module_basics.service.thesaurus_service import ThesaurusService, ThesaurusDataService
from utils.log_util import logger
from utils.page_util import PageResponseModel
from utils.response_util import ResponseUtil

thesaurusController = APIRouter(prefix='/basics/thesaurus', dependencies=[Depends(LoginService.get_current_user)])


@thesaurusController.get(
    '/catalogue/list',
    response_model=PageResponseModel,
    dependencies=[Depends(CheckUserInterfaceAuth('basics:thesaurus:list'))]
)
async def get_thesaurus_list(
        thesaurus_type_page_query: ThesaurusPageQueryModel = Depends(ThesaurusPageQueryModel.as_query),
        query_db: AsyncSession = Depends(get_db),
):
    # 获取分页数据
    dict_type_page_query_result = await ThesaurusService.get_thesaurus_list_services(
        query_db, thesaurus_type_page_query, is_page=True
    )
    logger.info('获取成功')

    return ResponseUtil.success(model_content=dict_type_page_query_result)


@thesaurusController.post('/catalogue', dependencies=[Depends(CheckUserInterfaceAuth('basics:thesaurus:add'))])
@ValidateFields(validate_model='add_thesaurus_catalogue')
@Log(title='专业词库', business_type=BusinessType.INSERT)
async def add_thesaurus_catalogue_info(
        request: Request,
        thesaurus_catalogue: ThesaurusModel,
        query_db: AsyncSession = Depends(get_db),
        current_user: CurrentUserModel = Depends(LoginService.get_current_user),
):
    thesaurus_catalogue.create_by = current_user.user.user_name
    thesaurus_catalogue.create_time = datetime.now()
    thesaurus_catalogue.update_by = current_user.user.user_name
    thesaurus_catalogue.update_time = datetime.now()
    add_thesaurus_catalogue_result = await ThesaurusService.add_thesaurus_catalogue_services(request,
                                                                                             query_db,
                                                                                             thesaurus_catalogue)
    logger.info(add_thesaurus_catalogue_result.message)

    return ResponseUtil.success(msg=add_thesaurus_catalogue_result.message)


@thesaurusController.put('/catalogue', dependencies=[Depends(CheckUserInterfaceAuth('basics:thesaurus:edit'))])
@ValidateFields(validate_model='edit_thesaurus_catalogue')
@Log(title='专业词库', business_type=BusinessType.UPDATE)
async def edit_thesaurus_catalogue_info(
        request: Request,
        edit_thesaurus_catalogue: ThesaurusModel,
        query_db: AsyncSession = Depends(get_db),
        current_user: CurrentUserModel = Depends(LoginService.get_current_user),
):
    edit_thesaurus_catalogue.update_by = current_user.user.user_name
    edit_thesaurus_catalogue.update_time = datetime.now()
    edit_thesaurus_catalogue_result = await ThesaurusService.edit_thesaurus_catalogue_services(request, query_db,
                                                                                               edit_thesaurus_catalogue)
    logger.info(edit_thesaurus_catalogue_result.message)

    return ResponseUtil.success(msg=edit_thesaurus_catalogue_result.message)


@thesaurusController.delete('/catalogue/{dict_ids}', dependencies=[Depends(CheckUserInterfaceAuth('basics:thesaurus'
                                                                                                  ':remove'))])
@Log(title='专业词库', business_type=BusinessType.DELETE)
async def delete_thesaurus_catalogue_info_by_ids(request: Request, thesaurus_ids: str,
                                                 query_db: AsyncSession = Depends(get_db)):
    delete_thesaurus_catalogue = DeleteThesaurusModel(thesaurusIds=thesaurus_ids)
    delete_thesaurus_catalogue_result = await ThesaurusService.delete_thesaurus_catalogue_services(request, query_db,
                                                                                                   delete_thesaurus_catalogue)
    logger.info(delete_thesaurus_catalogue_result.message)

    return ResponseUtil.success(msg=delete_thesaurus_catalogue_result.message)


@thesaurusController.get(
    '/data/list', response_model=ThesaurusModel, dependencies=[Depends(CheckUserInterfaceAuth('basics:thesaurus:list'))]
)
async def get_thesaurus_data_list(
        thesaurus_data_page_query: ThesaurusDataPageQueryModel = Depends(ThesaurusDataPageQueryModel.as_query),
        query_db: AsyncSession = Depends(get_db),
):
    # 获取分页数据
    thesaurus_data_page_query_result = await ThesaurusDataService.get_thesaurus_data_list_services(
        query_db, thesaurus_data_page_query, is_page=True
    )
    logger.info('获取成功')

    return ResponseUtil.success(model_content=thesaurus_data_page_query_result)


@thesaurusController.post('/data', dependencies=[Depends(CheckUserInterfaceAuth('basics:thesaurus:add'))])
@ValidateFields(validate_model='add_thesaurus_data')
@Log(title='专业词库', business_type=BusinessType.INSERT)
async def add_thesaurus_data_info(
        request: Request,
        add_thesaurus_data: ThesaurusDataModel,
        query_db: AsyncSession = Depends(get_db),
        current_user: CurrentUserModel = Depends(LoginService.get_current_user),
):
    add_thesaurus_data.create_by = current_user.user.user_name
    add_thesaurus_data.create_time = datetime.now()
    add_thesaurus_data.update_by = current_user.user.user_name
    add_thesaurus_data.update_time = datetime.now()
    add_thesaurus_data_result = await ThesaurusDataService.add_thesaurus_data_services(request, query_db,
                                                                                       add_thesaurus_data)
    logger.info(add_thesaurus_data_result.message)

    return ResponseUtil.success(msg=add_thesaurus_data_result.message)


@thesaurusController.put('/data', dependencies=[Depends(CheckUserInterfaceAuth('basics:thesaurus:edit'))])
@ValidateFields(validate_model='edit_thesaurus_data')
@Log(title='专业词库', business_type=BusinessType.UPDATE)
async def edit_thesaurus_data_info(
        request: Request,
        edit_thesaurus_data: ThesaurusDataModel,
        query_db: AsyncSession = Depends(get_db),
        current_user: CurrentUserModel = Depends(LoginService.get_current_user),
):
    edit_thesaurus_data.update_by = current_user.user.user_name
    edit_thesaurus_data.update_time = datetime.now()
    edit_thesaurus_data_result = await ThesaurusDataService.edit_thesaurus_data_services(request, query_db,
                                                                                         edit_thesaurus_data)
    logger.info(edit_thesaurus_data_result.message)

    return ResponseUtil.success(msg=edit_thesaurus_data_result.message)


@thesaurusController.delete('/data/{data_ids}',
                            dependencies=[Depends(CheckUserInterfaceAuth('basics:thesaurus:remove'))])
@Log(title='专业词库', business_type=BusinessType.DELETE)
async def delete_thesaurus_data_info(request: Request, data_ids: str, query_db: AsyncSession = Depends(get_db)):
    delete_thesaurus_data = DeleteThesaurusDataModel(dataIds=data_ids)
    delete_thesaurus_data_result = await ThesaurusDataService.delete_thesaurus_data_services(request, query_db,
                                                                                             delete_thesaurus_data)
    logger.info(delete_thesaurus_data_result.message)

    return ResponseUtil.success(msg=delete_thesaurus_data_result.message)
